<template>
  <div class="dialog">
    <el-dialog :title="title" :visible.sync="visible" :close-on-click-modal="false" width="65%">
      <el-form :model="createForm" label-width="120px" label-suffix=":" :rules="rules" ref="createForm">
        <div class="page-panel-title" style="margin-top: 0">技能信息</div>
        <div class="page-panel">
          <el-row :gutter="24">
            <el-col :span="12">
              <el-form-item label="技能名称" prop="skillTypeIdPath">
                <el-cascader :key="resetCascader" placeholder="请选择技能名称" v-model="createForm.skillTypeIdPath"
                  :props="props" style="width: 100%" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="单价类别" prop="skillPriceType">
                <el-select v-model="createForm.skillPriceType" placeholder="请选择单价类别" style="width: 100%">
                  <el-option label="时薪" value="0" />
                  <el-option label="件薪" value="1" />
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="24">
            <el-col :span="24">
              <el-form-item label="技能说明" prop="skillDesc">
                <el-input v-model.trim="createForm.skillDesc" placeholder="请输入技能说明" maxLength="50"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
        </div>
        <div v-if="showLevel">
          <div class="page-panel-title">等级信息</div>
          <div class="page-pane2">
            <el-button type="primary" size="mini" @click="initLevel()">查询</el-button>
            <el-button size="mini" type="info" @click="addLevel()">新增</el-button>
          </div>
          <el-table border style="margin-top: 10px" ref="multipleTable" stripe height="250px" :data="levelList">
            <el-table-column label="序号" type="index" width="50" align="center"></el-table-column>
            <el-table-column label="等级名称" prop="skillLevel" align="center" show-overflow-tooltip></el-table-column>
            <el-table-column label="等级单价" prop="skillPrice" align="center" show-overflow-tooltip></el-table-column>
            <el-table-column label="等级说明" prop="levelDesc" width="350" align="center"
              show-overflow-tooltip></el-table-column>
            <el-table-column label="操作" align="center" width="180">
              <template slot-scope="scope">
                <el-button type="text" @click="levelEdit(scope.row)">编辑</el-button>
                <!-- <el-divider direction="vertical"></el-divider> -->
                <!-- <el-button type="text" @click="levelDelete(scope.row)">删除</el-button> -->
              </template>
            </el-table-column>
          </el-table>
        </div>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="closeNetWorker()">关 闭</el-button>
        <el-button type="primary" @click="confirmData('createForm')">保 存</el-button>
      </div>

      <el-dialog :title="levelTitle" :visible.sync="visibleLevel" :before-close="closeLevel" :close-on-click-modal="false"
        width="45%" append-to-body>
        <el-form :model="levelForm" label-width="160px" label-suffix=":" :rules="levelRules" ref="levelForm">
          <el-row :gutter="24">
            <el-col :span="22">
              <el-form-item label="等级名称" prop="skillLevel">
                <el-input v-model.trim="levelForm.skillLevel" placeholder="请输入等级名称"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="24">
            <el-col :span="22">
              <el-form-item label="等级单价" prop="skillPrice">
                <el-input v-model.trim="levelForm.skillPrice" placeholder="请输入等级单价"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="24">
            <el-col :span="22">
              <el-form-item label="等级说明" prop="levelDesc">
                <el-input v-model.trim="levelForm.levelDesc" placeholder="请输入等级说明"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="closeLevel()">关 闭</el-button>
          <el-button type="primary" @click="confirmLevel('levelForm')">保 存</el-button>
        </div>
      </el-dialog>
    </el-dialog>
  </div>
</template>
<script>
import {
  storeSkillTypeLazyTree,
  storeSkillTypeAdd,
  storeSkillTypeUpdate,
  storeSkillTypeLevelList,
  storeSkillTypeLevelSave,
  storeSkillTypeLevelDel,
} from "@/api/index";
import { validateF2 } from '@/utils/validate'
export default {
  name: "SkillDialog",
  props: {
    exterOrg: {
      type: Object,
      default: {}
    },
  },
  data () {
    return {
      title: "",
      levelTitle: "",
      visible: false,
      showLevel: false,
      visibleLevel: false,
      createForm: {},
      allTree: [],
      defaultProps: {
        children: "children",
        label: "resourceName",
      },
      resetCascader: 0,
      props: {
        lazy: true,
        lazyLoad: (node, resolve) => {
          setTimeout(async () => {
            const res = await this.getTreeByParentCode(node);
            return resolve(res);
          });
        },
      },
      levelList: [],
      levelForm: {},
      rules: {
        skillTypeIdPath: [{ required: true, message: "请选择技能名称", trigger: "blur" }],
        skillPriceType: [{ required: true, message: "请选择单价类别", trigger: "blur" }],
      },
      levelRules: {
        skillLevel: [{ required: true, message: "请输入等级名称", trigger: "blur" }],
        skillPrice: [{ required: true, message: "请输入等级单价", trigger: "blur" },
        { validator: validateF2, trigger: 'blur' }],
      },
    };
  },
  created () { },
  mounted () { },
  methods: {
    add () {
      this.title = "新增";
      this.createForm = {};
      this.showLevel = false;
      this.resetCascader++;
      this.visible = true;
    },
    edit (val) {
      this.title = "编辑";
      this.createForm = Object.assign({}, val);
      this.showLevel = true;
      this.resetCascader++;
      this.initLevel();
      this.visible = true;
    },
    getTreeByParentCode (node) {
      return new Promise((resolve, reject) => {
        let params = {};
        if (node.data) {
          params = { parentId: node.data.value };
        }
        storeSkillTypeLazyTree(params).then((res) => {
          if (res.data.code == 0) {
            const nodes = Array.from(res.data.data).map((item) => ({
              value: item.id,
              label: item.skillTypeName,
              leaf: !item.hasChildren,
            }));
            resolve(nodes);
          }
        });
      });
    },
    initLevel () {
      let data = {
        skillTypeId: this.createForm.id,
        storeId: this.exterOrg.id
      }
      storeSkillTypeLevelList(data).then((res) => {
        if (res.data.code == 0) {
          this.levelList = res.data.data;
        }
      });
    },
    confirmData (formData) {
      this.$refs[formData].validate((valid) => {
        if (valid) {
          this.createForm.skillTypeId = this.createForm.skillTypeIdPath[
            this.createForm.skillTypeIdPath.length - 1
          ];
          if (this.showLevel) {
            storeSkillTypeUpdate(this.createForm).then((res) => {
              const data = res.data;
              if (data.code == 0) {
                this.$message.success("保存成功");
                this.$emit("ok");
                this.closeNetWorker();
              } else {
                this.$message.error(data.data || "保存成功");
              }
            });
          } else {
            this.createForm.storeId = this.exterOrg.id
            storeSkillTypeAdd(this.createForm).then((res) => {
              const data = res.data;
              if (data.code == 0) {
                this.$message.success("新增成功，请继续添加技能等级");
                this.createForm.id = data.data;
                this.showLevel = true;
                this.initLevel();
              } else {
                this.$message.error(data.data || "新增失败");
              }
            });
          }
        }
      });
    },
    closeNetWorker () {
      this.createForm = {};
      this.visible = false;
    },
    addLevel () {
      this.levelTitle = "新增等级";
      this.visibleLevel = true;
    },
    levelEdit (val) {
      this.levelTitle = "编辑等级";
      this.levelForm = Object.assign({}, val);
      this.visibleLevel = true;
    },
    confirmLevel (formData) {
      this.$refs[formData].validate((valid) => {
        if (valid) {
          this.levelForm.skillTypeId = this.createForm.id;
          this.levelForm.storeId = this.exterOrg.id;
          storeSkillTypeLevelSave(this.levelForm).then((res) => {
            const data = res.data;
            if (data.code == 0) {
              this.$message.success("保存成功");
              this.initLevel();
              this.closeLevel();
            } else {
              this.$message.error(data.data || "保存成功");
            }
          });
        }
      });
    },
    levelDelete (val) {
      let me = this;
      this.$confirm("确定删除该数据?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          let params = [];
          params.push(val.id);
          storeSkillTypeLevelDel(params)
            .then((res) => {
              let data = res.data;
              if (data && data.code == 0) {
                me.$message.success("删除成功");
                this.initLevel();
              }
            })
            .catch(() => {
              me.$message.error("请求失败");
            });
        })
        .catch(() => { });
    },
    closeLevel () {
      this.levelForm = {};
      this.visibleLevel = false;
    },
  },
};
</script>
<style scoped lang="less">
.content {
  height: 400px;
  overflow: auto;
}
</style>
